
cc.Class({
    extends: cc.Component,

    properties: {
        TogPre: cc.Prefab, //预制件
        spChecks: [cc.SpriteFrame], //icon
        gameCreateConfig: cc.JsonAsset, //创建房间的json 配置文件 
        node_RightLayout:cc.Node,
    },


    onLoad () {
        this.Toggles = [];
    },

    start () {
        this.RefreshAllToggles();
    },

    RefreshAllToggles(){
        let josn = this.gameCreateConfig.json;
        for (let key in josn) {
            let node = null;
            let dataKey = josn[key].Key;
            let toggleCount = josn[key].ToggleCount;
            let AtRows = josn[key].AtRow.toString().split(','); //每个子选项按钮的列数布局
            let spacing = josn[key].Spacing.toString().split(','); //间隔

            //创建单行
            node = cc.instantiate(this.TogPre);
            node.active = true;
            //需要判断添更加多的Toggle
            let addCount = toggleCount - 1;
            if (addCount < 0)
                this.ErrLog('gameCreate Config ToggleCount error');
            else {
                for (let i = 2; i <= toggleCount; i++) {
                    let prefabNode = node.getChildByName('Toggle1');
                    let newNode = cc.instantiate(prefabNode);
                    newNode.name = 'Toggle' + i;
                    node.addChild(newNode);
                }
            }

            node.name = 'Toggles_' + dataKey;
            node.x = 10;

            if (!this.Toggles[dataKey])
            this.Toggles[dataKey] = [];

            let fristPos = { x: 0, y: 0 };
            let lastPos = { x: 0, y: 0 };
            for (let i = 1; i <= toggleCount; i++) {
                let curNode = node.getChildByName('Toggle' + i); //单行的子选项
                curNode.getComponent("Data").isFirstNode = false;
                if (curNode) {
                    //位置宽高设置下
                    //记录下第一个的位置方便换行
                    if (1 == i) {
                        fristPos.x = curNode.x;
                        fristPos.y = curNode.y;
                        lastPos.x = curNode.x;
                        lastPos.y = curNode.y;
                        // curNode.getComponent("Data").isFirstNode = true;
                    }
                    else if (1 < i) {//第1个以后都是新增的
                        // if (AtRows[i - 2] != AtRows[i - 1]) {
                        //     curNode.x = fristPos.x;
                        //     curNode.y = lastPos.y - curNode.height - this.rightPrefabSpacing;
                        //     node.height = node.height + curNode.height + this.rightPrefabSpacing;
                        //     // curNode.getComponent("Data").isFirstNode = true;
                        // }
                        // else {
                            curNode.x = lastPos.x + parseInt(spacing[i - 1]);
                            curNode.y = lastPos.y;
                        // }
                    }
                    lastPos.x = curNode.x;
                    lastPos.y = curNode.y;

                    // // curNode.on(cc.Node.EventType.TOUCH_START, this.OnToggleClick, this);
                    // let checkNode = curNode.getChildByName('checkmark');
                    // //默认不勾选
                    // checkNode.active = false;
                    // let icon_selectBg = curNode.getChildByName('icon_selectBg');
                    // let showList = josn[key].ShowIndexs.toString().split(',');
                    //尝试获取缓存
                    // let clubId = 0;
                    // let roomKey = '0';
                    // let unionId = 0;
                    // let unionRoomKey = '0';
                    // let linshi = null;
                    // if (this.clubData) {
                    //     clubId = this.clubData.clubId;
                    //     roomKey = this.clubData.gameIndex;
                    //     linshi = this.GetLocalConfig(josn[key].Key, clubId, roomKey, unionId, unionRoomKey);
                    // }
                    // //如果cfg没有的话，就是新建房间，才读本地
                    // if (this.unionData != null && this.unionData.cfgData == null) {
                    //     clubId = this.unionData.clubId;
                    //     unionId = this.unionData.unionId;
                    //     unionRoomKey = this.unionData.roomKey;
                    //     linshi = this.GetLocalConfig(josn[key].Key, clubId, roomKey, unionId, unionRoomKey);
                    // }
                    //如果cfg没有的话，就是新建房间，才读本地
                    // if (this.unionData == null || this.unionData.cfgData == null) {
                    //     //第一次创建俱乐部房间没有roomKey为0
                    //     if (!linshi)
                    //         linshi = this.GetLocalConfig(josn[key].Key, clubId, '0', unionId, unionRoomKey);
                    //     if (linshi) {
                    //         let linshiList = linshi.split(',');
                    //         for (let j = 0; j < linshiList.length; j++) {//缓存可能出BUG(配置删除了按钮数量)
                    //             if (parseInt(linshiList[j]) > toggleCount) {
                    //                 linshiList = ['1'];
                    //                 break;
                    //             }
                    //         }
                    //         showList = linshiList;
                    //     }
                    // } else {
                    //     let cfgDataList = this.unionData.cfgData.bRoomConfigure[dataKey];
                    //     if (typeof (cfgDataList) == "object") {
                    //         showList = [];
                    //         for (let j = 0; j < cfgDataList.length; j++) {
                    //             //索引要加1
                    //             let realIndex = cfgDataList[j] + 1;
                    //             showList.push(realIndex);
                    //         }
                    //     } else if (typeof (cfgDataList) == "number") {
                    //         //单选，就一个数字，加入数组
                    //         let showListTemp = [];
                    //         //索引要加1
                    //         showListTemp.push(cfgDataList + 1);
                    //         showList = showListTemp;
                    //     }
                    // }

                    // if (this.clubData && 'fangfei' == dataKey)
                    //     showList = [1];
                    // if (this.unionData && 'fangfei' == dataKey)
                    //     showList = [1];

                    //尝试获取缓存
                    // if (0 == josn[key].ToggleType && 1 != showList.length) //单选
                    //     // this.ErrLog('gameCreate Config ToggleType and ShowIndexs error');

                    // if (1 == josn[key].ToggleType) {//多选的图片设置下(不放上面是因为路径)
                    //     node.addComponent(cc.Toggle);
                    //     checkNode.getComponent(cc.Sprite).SpriteFrame = this.spChecks[3];
                    //     icon_selectBg.getComponent(cc.Sprite).SpriteFrame = this.spChecks[1];
                    // }

                    // //输入框
                    // if (2 === josn[key].ToggleType) {//显示输入框
                    //     console.log("======显示输入框========",josn[key]);
                    //     // let icon_selectBg = curNode.getChildByName("icon_selectBg");
                    //     // icon_selectBg.active = false;
                    //     // curNode.getChildByName("checkmark").active = false;
                    //     // curNode.getChildByName("label").active = false;

                    //     // let editboxEX = curNode.getChildByName("editboxEX");
                    //     // editboxEX.active = true;
                    //     // editboxEX.anchorX = 0; 
                    //     // editboxEX.x = icon_selectBg.x + editboxEX.width/2;
                    //     // editboxEX.getComponent(cc.EditBox).string = josn[key].ShowIndexs;
                    //     // // //纪录区间
                    //     // let minAndMax = josn[key].ToggleDesc.toString().split('-');
                    //     // if(minAndMax.length > 0){
                    //     //     editboxEX.minEnter = parseInt(minAndMax[0]);
                    //     //     editboxEX.maxEnter = parseInt(minAndMax[1]);
                    //     // }
                    // }

                    // for (let j = 0; j < showList.length; j++) {
                    //     if (i == parseInt(showList[j])) {
                    //         checkNode.active = true;
                    //         break;
                    //     }
                    //     else {
                    //         checkNode.active = false;
                    //     }
                    // }
                    this.Toggles[dataKey].push(curNode);
                }
            }

            this.node_RightLayout.addChild(node);
           
        }
    },

    //获取创建缓存
    GetLocalConfig: function (configName, clubId, roomKey, unionId, unionRoomKey) {
        return cc.sys.localStorage.getItem(this.gameType + '_' + clubId + '_' + roomKey + '_' + unionId + '_' + unionRoomKey + '_' + configName);
    },

    OnToggleClick: function (event) {
        // this.FormManager.CloseForm("UIMessageTip");
        // let toggles = event.target.parent;
        // let toggle = event.target;
        // let key = toggles.name.substring(('Toggles_').length, toggles.name.length);
        // let toggleIndex = parseInt(toggle.name.substring(('Toggle').length, toggle.name.length)) - 1;
        // let needClearList = [];
        // let needShowIndexList = [];
        // needClearList = this.Toggles[key];
        // needShowIndexList.push(toggleIndex);
        // if ('jushu' == key || 'renshu' == key || 'fangfei' == key) {
        //     this.ClearToggleCheck(needClearList, needShowIndexList);
        //     this.UpdateLabelColor(toggles);
        //     this.UpdateTogglesLabel(toggles, false);
        //     return;
        // } else if ('kexuanwanfa2' == key) {
        //     if (this.Toggles['kexuanwanfa2'][4].getChildByName('checkmark').active && toggleIndex == 5) {
        //         this.Toggles['kexuanwanfa2'][4].getChildByName('checkmark').active = false;
        //         this.UpdateLabelColor(this.Toggles['kexuanwanfa2'][4].parent);
        //     } else if (this.Toggles['kexuanwanfa2'][5].getChildByName('checkmark').active && toggleIndex == 4) {
        //         this.Toggles['kexuanwanfa2'][5].getChildByName('checkmark').active = false;
        //         this.UpdateLabelColor(this.Toggles['kexuanwanfa2'][5].parent);
        //     }
        // }
        // if (toggles.getComponent(cc.Toggle)) {//复选框
        //     needShowIndexList = [];
        //     for (let i = 0; i < needClearList.length; i++) {
        //         let mark = needClearList[i].getChildByName('checkmark').active;
        //         //如果复选框为勾选状态并且点击的复选框不是该复选框，则继续保持勾选状态
        //         if (mark && i != toggleIndex) {
        //             needShowIndexList.push(i);
        //         }
        //         //如果复选框为未勾选状态并且点击的复选框是该复选框，则切换为勾选状态
        //         else if (!mark && i == toggleIndex) {
        //             needShowIndexList.push(i);
        //         }
        //     }
        // }
        // this.ClearToggleCheck(needClearList, needShowIndexList);
        // this.UpdateLabelColor(toggles, 'fangfei' == key ? true : false);
        // this.UpdateOnClickToggle();
    },
});
